<template>
  <div>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-button type="warning" style="margin-left: 50px" @click="handleadd"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 支付方式信息列表 -->
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="id" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型名" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.weChat }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片显示" width="300" height="300">
        <template slot-scope="scope">
          <!-- //动弹绑定src并拼接获取到的图片路径 -->
          <img
            v-if="scope.row.alipay"
            :src="
              currentAddresses + '/files/picPath?picUrl=' + scope.row.alipay
            "
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column label="是否启用" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">
            <el-switch
              @change="handdisable(scope.$index, scope.row)"
              v-model="scope.row.isActived"
            >
            </el-switch>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- Dialog对话框 -->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible">
      <el-form :model="formData">
        <el-form-item label="二维码类型名" label-width="100px">
          <el-input
            v-model="formData.payName"
            style="width: 400px"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-upload
          :headers="uploadActionHeaders"
          :action="uploadActionUrl"
          :on-success="handleAvatarSuccess"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :url="imageUrl"
          :before-upload="beforeAvatarUpload"
          :multiple="false"
        >
          <img v-if="formData.payUrl" :src="formData.payUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        :current-page="pager.currentPage"
        :page-size="pager.pageSize"
        layout="prev, pager, next, jumper,sizes, total"
        :total="pager.totalSize"
        :page-sizes="[5, 10, 15, 20]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>


<script>
import { getList, addqrcode, modqrcode, delqrcode } from "../api/qrcode";
export default {
  props: {
    isActived: {
      type: Boolean,
    },
  },
  data() {
    return {
      currentAddresses: "http://localhost:5000",
      imageUrl: "",
      //初始化一些数据
      tableData: [],

      currentIndex: 0,
      dialogFormVisible: false,
      pager: {
        //每页显示条目个数
        pageSize: 5,
        currentPage: 1,
        totalSize: 0,
      },
      formData: {
        id: 0,
        payName: "",
        payUrl: "",
        IsActived: true,
      },
      msg: "",
      curIndex: 0,
    };
  },
  methods: {
    //预览图片时
    handlePreview(file) {
      console.log(file);
    },
    //删除图片时
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.fileLength = 0;

      //显示上传按钮
      this.hiddenUploadBtn = false;
    },
    //文件上传成功
    handleAvatarSuccess(res, file) {
      this.formData.payUrl = URL.createObjectURL(file.raw);
      this.imageUrl = this.formData.payUrl;
    },
    //文章上传前验证
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //拉取数据
    fetchData() {
      getList(this.pager)
        .then((res) => {
          console.log(res);
          this.tableData = res.data.data;
          this.pager = res.data.pager;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 编辑
    handleEdit(index, row) {
      this.formData.id = row.id;
      this.dialogFormVisible = true;
      this.formData.payName = row.weChat,
      this.formData.payUrl = this.currentAddresses + "/files/picPath?picUrl=" + row.alipay,
      this.formData.IsActived = row.isActived;
      this.curIndex = index;

      console.log(this.formData);
    },
    // 删除
    handleDelete(index, row) {
      console.log(index, row);
      console.log(row.id);
      //通过row.id找到当前的id
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delqrcode(row.id).then(({ data }) => {
            console.log(data);
            this.tableData.splice(index, 1);
            this.pager.totalSize -= 1;
            let nun = parseInt(this.pager.totalSize / this.pager.pageSize) + 1;
            if (
              //判断取余后为零 pager.pageindex-1
              parseInt(this.pager.totalSize % this.pager.pageSize) === 0 &&
              this.pager.currentPage === nun
            ) {
              this.pager.currentPage -= 1;
            }
            //重新拉取
            this.fetchData();
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 新增
    handleadd() {
      this.dialogFormVisible = true;
      this.formData.id = 0;
      this.formData.payName = "";
    },
    // 取消按钮
    handleCancel() {
      this.dialogFormVisible = false;
    },
    // 保存按钮
    handleSave() {
      this.dialogFormVisible = false;
      console.log(this.formData);

      //新增
      if (this.formData.id === 0) {
        console.log(this.formData);
        addqrcode(this.formData).then((res) => {
          console.log(res);
          this.tableData.push(res.data);
          this.fetchData();
        });
      } else {
        modqrcode(this.formData.id, this.formData).then((res) => {
          // console.log(res);
          this.tableData.splice(this.curIndex, 1, res.data);
          console.log(this.formData);
          this.fetchData();
        });
      }
    },
    // 通过页码来拉取数据
    handleCurrentChange(val) {
      this.pager.currentPage = val;
      this.fetchData();
    },
    // 改变页大小来拉取数据
    handleSizeChange(val) {
      this.pager.pageSize = val;
      this.fetchData();
    },
    //是否开启
    handdisable(index, row) {
      console.log(index);
      console.log(row);
      this.formData.IsActived = !this.formData.IsActived;
      console.log(this.formData);

      // this.formdata.id = row.id;
      // this.formdata.username = row.username;
      // this.formdata.password = row.password;

      // console.log(this.formdata);

      // modUser(this.formdata.id, this.formdata)
      //   .then((res) => {
      //     console.log(res);
      //   })
      //   .catch((err) => {
      //     console.log(err);
      //   });
    },
  },
  computed: {
    uploadActionHeaders() {
      return {
        Authorization: "Bearer " + "token",
      };
    },
    // 图片上传路径
    uploadActionUrl() {
      let baseUrl = "http://localhost:5000";
      let url = "/files/UploadPayment";
      return baseUrl + url;
    },
  },
  created() {
    this.fetchData();
  },
};
</script>



<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
img {
  width: 178px;
}
</style>


